import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_easyrefresh/bezier_circle_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:flutter_wan_android/blocs/project_tree_bloc.dart';
import 'package:flutter_wan_android/blocs/wechat_bloc.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/dimens.dart';
import 'package:flutter_wan_android/res/strings.dart';
import 'package:flutter_wan_android/ui/home_page.dart';
import 'package:flutter_wan_android/ui/project_page.dart';
import 'package:flutter_wan_android/ui/wechat_page.dart';

class MainPage extends StatefulWidget {
  static final String sName = "/";

  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}

class _MainPageState extends State<MainPage> {
  List<Widget> _bodyList = <Widget>[HomePage(),
    BlocProvider<ProjectTreeBloc>(bloc: ProjectTreeBloc(), child: ProjectPage()),
    BlocProvider<WeChatBloc>(bloc: WeChatBloc(),child: WeChatPage(),)];
  List<String> titles = <String>[
    Strings.mainTabHomeText,
    Strings.mainTabProjectText,
    Strings.mainTabTencentText
  ];

  List<Map<Icon, Icon>> _icons = List();

  int _index = 0;

  Widget getIcon(IconData iconData, Color color) {
    if (color == null) {
      color = XColors.mainTabSelectedColor;
    }
    return Icon(
      iconData,
      size: XDimens.bottomNavIconSize,
      color: color,
    );
  }

  @override
  void initState() {
    super.initState();
    ClassicalFooter footer=   ClassicalFooter(
      loadingText: '加载中…',
      loadedText: '加载完成',
      loadFailedText: '加载失败',
      noMoreText: '- 已经到底了 -',
      infoText: '更新于%T',
    );


    SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
      EasyRefresh.defaultHeader=BezierCircleHeader(backgroundColor: XColors.primaryColor);
      EasyRefresh.defaultFooter=footer;
    });

    for (var i = 0; i < 3; i++) {
      Map<Icon, Icon> map = Map();
      switch (i) {
        case 0:
          map[getIcon(Icons.home, null)] =
              getIcon(Icons.home, XColors.mainTabUnSelectedColor);
          break;
        case 1:
          map[getIcon(Icons.widgets, null)] =
              getIcon(Icons.widgets, XColors.mainTabUnSelectedColor);

          break;
        case 2:
          map[getIcon(Icons.supervisor_account, null)] =
              getIcon(Icons.supervisor_account, XColors.mainTabUnSelectedColor);
          break;
      }
      _icons.add(map);
    }
  }

  Icon getIndexImage(int index) {
    Map itemMap = _icons[index];
    if (index == _index) {
      return itemMap.keys.toList()[0];
    } else {
      return itemMap.values.toList()[0];
    }
  }

  BottomNavigationBarItem getBottomNavigationBarItem(int index) {
    return BottomNavigationBarItem(
        icon: getIndexImage(index),
        title: Text(titles[index]),
        activeIcon: getIndexImage(index));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
          index: _index,
          alignment: AlignmentDirectional.center,
          children: _bodyList),
      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          getBottomNavigationBarItem(0),
          getBottomNavigationBarItem(1),
          getBottomNavigationBarItem(2)
        ],
        currentIndex: _index,
        backgroundColor: XColors.bottomNavBgColor,
        selectedItemColor: XColors.mainTabSelectedColor,
        unselectedItemColor: XColors.mainTabUnSelectedColor,
        onTap: (int index) {
          setState(() {
            _index = index;
          });
        },
      ),
    );
  }
}
